﻿#Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
	Add-PSSnapin Microsoft.SharePoint.Powershell
}

$webApps = Get-SPWebApplication
foreach ($webApp in $webApps) {
	$database = Get-SPContentDatabase -WebApplication $webApp
    # Test-SPContentDatabase
    # https://technet.microsoft.com/en-us/library/ff607948.aspx
    # http://blogs.msdn.com/b/subbu/archive/2012/05/19/little-more-about-test-spcontentdatabase-cmdlet.aspx
    # http://blogs.msdn.com/b/subbu/archive/2012/05/20/fixing-test-spcontentdatabase-warnings.aspx
    $result = Test-SPContentDatabase $database 
	if($result.Length -gt 0) {
		$result > $database.Name
	}
}

# How to identify and clean up orphan sites and orphan contentdatabases in SharePoint
# http://social.technet.microsoft.com/wiki/contents/articles/3837.how-to-identify-and-clean-up-orphan-sites-and-orphan-contentdatabases-in-sharepoint.aspx
Remove-SPSite -Identity "00000000-0000-0000-0000-000000000000"
$database = Get-SPContentDatabase -WebApplication http://siteUrl
Dismount-SPContentDatabase $database
Mount-SPContentDatabase $database.Name -DatabaseServer $database.Server -WebApplication $database.WebApplication
# stsadm -o deleteconfigurationobject -id "00000000-0000-0000-0000-000000000000"